from sqlalchemy import * 
from sqlalchemy.orm import * 
import datetime 
from config import * 
from users import user 
from news import news 
from events import event 
from eventrsvps import eventrsvp 
from polls import poll 
from polloptions import polloption 
from pollresponses import pollresponse 
from documents import document 
from galleries import gallery
from images import image
try:
    connection = config.get('db', 'connection')
except:
    print 'No Database Specified'
    #sys.exit(1)
engine = create_engine(connection)
metadata = MetaData(engine)
Session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))



user_table = Table('USERS', metadata, 
	Column('USERID', String(128), primary_key=True), 
	Column('PASSWORD', String(128), info={"type":"password"}), 
	Column('EMAIL', String(128)), 
	Column('ADDRESS1', String(256)), 
	Column('ADDRESS2', String(256)), 
	Column('CITY', String(256)), 
	Column('ZIP', String(256)), 
	Column('JOIN', Date, info={"datatype":"datetime"}), 
	Column('TITLE', String(256)), 
	Column('ABOUT', String(512), info={"datatype":"text"}), 
	Column('ADMIN', BOOLEAN, info={"datatype":"boolean"}))

news_table = Table('NEWS', metadata, 
	Column('NEWSID', Integer, autoincrement=True, primary_key=True, info={"visible":False}), 
	Column('AUTHORID', String(128), ForeignKey('USERS.USERID'), info={"lookup":"lookupownerids"}), 
	Column('TITLE', String(48)), 
	Column('NEWSDATE', Date, info={"datatype":"datetime"}), 
	Column('INFO', Text, info={"datatype":"text"}), 
	Column('PUBLIC', BOOLEAN, info={"datatype":"boolean"}))

event_table = Table('EVENTS', metadata, 
	Column('EVENTID', Integer, autoincrement=True, primary_key=True, info={"visible": False}), 
	Column('OWNERID', String(128), ForeignKey('USERS.USERID'), info={"lookup":"lookupownerids"}), 
	Column('TITLE', String(48)), 
	Column('EVENTDATE', Date, info={"datatype":"datetime"}), 
	Column('INFO', Text, info={"datatype":"text"}), 
	Column('PUBLIC', BOOLEAN, info={"datatype":"boolean"}))

eventrsvp_table = Table('EVENTRSVPS', metadata, 
	Column('RSVPID', Integer, autoincrement=True, primary_key=True), 
	Column('USERID', String(128), ForeignKey('USERS.USERID'), primary_key=True), 
	Column('EVENTID', Integer, ForeignKey('EVENTS.EVENTID')), 
	Column('NOTES', String(128)))

poll_table = Table('POLLS', metadata, 
	Column('POLLID', Integer, autoincrement=True, primary_key=True, info={"visible": False}), 
	Column('QUESTION', String(512)))

polloption_table = Table('POLLOPTIONS', metadata, 
	Column('OPTIONID', Integer, autoincrement=True, primary_key=True), 
	Column('POLLID', Integer, ForeignKey('POLLS.POLLID')), 
	Column('ANSWER', String(128)))

pollresponse_table = Table('POLLRESPONSES', metadata, 
	Column('RESPONSEID', Integer, autoincrement=True, primary_key=True), 
	Column('OPTIONID', Integer, ForeignKey('POLLOPTIONS.OPTIONID')))

document_table = Table('DOCUMENTS', metadata, 
	Column('DOCUMENTID', Integer, autoincrement=True, primary_key=True), 
	Column('TITLE', String(48)), 
	Column('LOCATION', String(256)), 
	Column('NEEDADMIN', BOOLEAN))

image_table = Table('IMAGES', metadata, 
	Column('IMAGEID', Integer, autoincrement=True, primary_key=True), 
	Column('TITLE', String(48)), 
	Column('LOCATION', String(256)))

gallery_table = Table('GALERIES', metadata, 
	Column('GALLERYID', Integer, autoincrement=True, primary_key=True), 
	Column('TITLE', String(48)), 
	Column('INFO', Text))

#need to add a table to tie images to galleries
mapper(user, user_table)
mapper(news, news_table)
mapper(event, event_table)
mapper(eventrsvp, eventrsvp_table)
mapper(poll, poll_table)
mapper(polloption, polloption_table)
mapper(pollresponse, pollresponse_table)
mapper(document, document_table)
mapper(image, image_table)
mapper(gallery, gallery_table)

def getsession():
    return Session()

def create_tables():
    metadata.create_all(engine)

def drop_tables():
    metadata.drop_all(engine)
